#!/usr/bin/env python
# -*- coding: utf-8 -*-

class BTree:
    def __init__(self, a, w):
        self.a = a
        self.w = w
        self.lt = None
        self.rt = None
        self.code = ""

def min(BTree):
    index = -1
    min = 100
    for i in range(len(btree)):
        if btree[i].w < min:
            min = btree[i].w
            index = i
    return index

print("输入模式：\n不重复的包含字母\n顺序对应的字母权重，空格分隔")
a = input()
n = len(a)
# w = []
# for i in a:
#     w.append(a.count(i))
w = input().split()
btree = []
for i in range(n):
    btree.append(BTree(a[i], int(w[i])))

while len(btree) > 1:
    lminBtree = btree.pop(min(btree))
    rminBtree = btree.pop(min(btree))
    btree.append(BTree(lminBtree.a + rminBtree.a, lminBtree.w + rminBtree.w))
    btree[len(btree) - 1].lt = lminBtree
    btree[len(btree) - 1].rt = rminBtree

stack = []
stack.append(btree[0])
while len(stack) != 0:
    item = stack.pop()
    if item.rt != None:
        stack.append(item.rt)
        item.rt.code = item.code + "1"
    if item.lt != None:
        stack.append(item.lt)
        item.lt.code = item.code + "0"

count = 0
dic = {}
stack = []
stack.append(btree[0])
while len(stack) != 0:
    item = stack.pop()
    if len(item.a) == 1:
        dic[item.a] = item.code
        count += len(item.code) * item.w
    if item.rt != None:
        stack.append(item.rt)
    if item.lt != None:
        stack.append(item.lt)

print("树总权重")
print(count)

for i in a:
    print(i + ":" + dic[i])